package com.nervenets.general.web.params;

import com.nervenets.general.hibernate.TreeDomainObject;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public abstract class TreeQueryParams<T extends TreeDomainObject<T>> extends QueryParams<T> {
    @ApiParam(value = "顶级对象Id，用于返回某个子树，默认传0", defaultValue = "0")
    private long parentId;
    @ApiParam(value = "是否返回所有，当为true时，返回所有，否则只返回启用的，默认传false", defaultValue = "false")
    private boolean all;

    public static <T extends TreeDomainObject<T>> TreeQueryParams<T> defaultInstance() {
        return new DefaultTreeQueryParams<>();
    }

    @Override
    public void generateCustomSpecification(List<Predicate> predicates, Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        super.generateCustomSpecification(predicates, root, query, builder);
        addEqualsPredicate(root.get("parentId"), parentId, true);
    }

    public abstract String cacheKey();
}
